using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using TeamXpandLicence.ObjectExchange;
using TXLooker.Common;
using TXLooker.Properties;

namespace TXLooker.License
{
    public class TrialLicenceCheck : ILicenceCheck
    {
        public TrialLicenceCheck()
        {
            LogManager.WriteMessageToCustomLog("Begin checking trial Licence");
            CheckLicence();
        }


        private int m_daysOff;
        public int DaysOff
        {
            get { return this.m_daysOff; }
            set { this.m_daysOff = value; }
        }

        private LicenceType m_licenceType;
        public LicenceType LicenceType
        {
            get { return this.m_licenceType; }
            set { this.m_licenceType = value; }
        }

        public void CheckLicence()
        {
            try
            {
                string sendString = string.Empty;
                using (MemoryStream MS = new MemoryStream())
                {
                    XmlSerializer formatter = new XmlSerializer(typeof(TrialLicenceDataObjectIn));
                    TrialLicenceDataObjectIn objectIn = new TrialLicenceDataObjectIn(ApplicationGlobal.MacAddress);

                    formatter.Serialize(MS, objectIn);
                    byte[] buffer = MS.GetBuffer();
                    sendString = Encoding.UTF8.GetString(buffer);
                }
                TrialLicenceDataObjectOut objOut = null;
                string requestString = Resources.STRING_LICENCE_URL + "CheckTrialLicence?TrialLicenceDataObjectIn=" + sendString;
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(requestString);
                using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
                {
                    using (Stream stream = webResponse.GetResponseStream())
                    {
                        byte[] buffer = new byte[webResponse.ContentLength];

                        stream.Read(buffer, 0, buffer.Length);
                        string retString = Encoding.UTF8.GetString(buffer);
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(retString);
                        retString = doc.InnerText;

                        using (TextReader textReader = new StringReader(retString))
                        {
                            XmlSerializer formatter = new XmlSerializer(typeof(TrialLicenceDataObjectOut));
                            objOut = (TrialLicenceDataObjectOut)formatter.Deserialize(textReader);
                        }
                    }
                }

                if (objOut == null)
                {
                    this.DaysOff = -1;
                    this.LicenceType = LicenceType.Error;
                }
                this.LicenceType = objOut.LicenceType;
                this.DaysOff = objOut.TimeLicenceLeft;
                if (this.DaysOff < 0)
                    this.LicenceType = LicenceType.TrialDaysOff;
            }
            catch (Exception ex)
            {
                LogManager.WriteMessageToCustomLog("Error in CheckLicence " + ex.ToString());
                this.DaysOff = -1;
                this.LicenceType = LicenceType.Error;
            }
        }
    }
}
